Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
event-to-promise
Advanced tools
Create a promise waiting for an event
Installation of the npm package:
> npm install --save event-to-promise
Then require the package:
var eventToPromise = require('event-to-promise');
You can directly use the build provided at unpkg.com:
<script src="https://unpkg.com/event-to-promise@0.8/dist/event-to-promise.js"></script>
function createServer (port) {
var server = require('http').createServer()
server.listen(port)
// The server will be returned once it has started listening.
//
// If an error happened, it will be forwarded instead.
return eventToPromise(server, 'listening').then(function () {
return server
})
}
// Using plain promise.
createServer(80).then(function (server) {
console.log('Server listening on http://localhost:80/')
}).catch(function (error) {
console.error('Server could not start:', error)
})
Event better using ES2016 asynchronous functions:
async function createServer (port) {
var server = require('http').createServer()
server.listen(port)
await eventToPromise(server, 'listening')
return server
}
async function main () {
try {
const server = await createServer(80);
console.log('Server listening on http://localhost:80/');
} catch (error) {
console.error('Server could not start:', error);
}
}
main()
This library requires promises support, for Node versions prior to 0.12 see this page to enable them.
Wait for one event. The first parameter of the emitted event is used to resolve/reject the promise.
The returned promise has a cancel()
method which can be used to
remove the event listeners. Note that the promise will never settled
if canceled.
const promise = eventToPromise(emitter, 'foo')
promise.cancel()
Required
Type: Object
The event emitter you want to watch an event on.
Required
Type: string
The name of the event you want to watch.
Type: boolean
Default: false
If true, all parameters of the emitted events are put in an array which is used to resolve/reject the promise.
Type: string
Default: "error"
The name of the event which rejects the promise.
Type: boolean
Default: false
Whether the error event should be ignored and not reject the promise.
Wait for one of multiple events. The array of all the parameters of the emitted event is used to resolve/reject the promise.
The array also has an
event
property indicating which event has been emitted.
The returned promise has a cancel()
method which can be used to
remove the event listeners. Note that the promise will never settled
if canceled.
const promise = eventToPromise(emitter, 'foo')
promise.cancel()
Required
Type: Object
The event emitter you want to watch an event on.
Required
Type: Array<string>
The names of the events which resolve the promise.
Type: Array<string>
Default: [ 'error' ]
The names of the events which reject the promise.
Contributions are very welcome, either on the documentation or on the code.
You may:
ISC © Julien Fontanet
FAQs
Create a promise waiting for an event
The npm package event-to-promise receives a total of 36,828 weekly downloads. As such, event-to-promise popularity was classified as popular.
We found that event-to-promise demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.